package com.shenhua.idm.provisioning.config;


import lombok.extern.slf4j.Slf4j;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;

@Component
@Slf4j
public class PublishEndpoint implements ApplicationRunner {

    @Autowired
    private WebApplicationContext applicationConnect;

    @Autowired()
    @Qualifier(Bus.DEFAULT_BUS_ID)
    private SpringBus bus;

    @SuppressWarnings("resource")
    @Override
    public void run(ApplicationArguments applicationArguments) throws Exception {
        log.info("开始进行自动发布webService接口");

        String[] beanNames = applicationConnect.getBeanNamesForAnnotation(AutoPublish.class);
        for(String beanName : beanNames) {
            String publishAddr = applicationConnect.getType(beanName).getAnnotation(AutoPublish.class).publishAddress();
            EndpointImpl endpoint = new EndpointImpl(bus, applicationConnect.getBean(beanName));
            endpoint.publish(publishAddr);

            log.info(String.format("发布接口地址：[%s]", publishAddr));
        }

        log.info("webService接口自动发布结束");
    }

}


